<?php

/**
 * 项目初始化类
 * @author keluo<pycorvn@yeah.net>
 * @since 2015-01-24
 */
final class SCProject {
    
    public function __construct() {
        $this->createDirectories();
        $this->createConfigFile();
        $this->createDefaultControllerFile();
        $this->createDefaultViewFile();
        $this->createDefaultStyleFile();
        $this->createDefaultWidgetFile();
        
        $index = new IndexController;
        $index->index();
    }
    
    private function createDirectories() {
        $dirs = array(
            SC_ASSET_PATH => 0777,
            SC_ASSET_PATH.'/css' => 0777,
            SC_ASSET_PATH.'/image' => 0777,
            SC_ASSET_PATH.'/js' => 0777,
            SC_CONFIG_PATH => 0775,
            SC_CONTROLLER_PATH => 0775,
            SC_MODEL_PATH => 0775,
            SC_VIEW_PATH => 0775,
            SC_VIEW_PATH.'/index' => 0775,
            SC_VIEW_PATH.'/widget' => 0775,
            SC_VENDOR_PATH => 0775,
            SC_LOG_PATH => 0775,
        );
        foreach($dirs as $dir => $mode) {
            (file_exists($dir) && is_dir($dir)) || mkdir($dir, $mode, true);
        }
    }
    
    private function createConfigFile() {
        $tpl = '<?php'.$this->n(2).
        '/**'.$this->n().
        ' * 自定义配置文件'.$this->n().
        ' */'.$this->n().
        'return array('.$this->n(2).
        $this->t().'// 开启rewrite模式'.$this->n().
        $this->t().'\'rewrite\' => true,'.$this->n(2).
        $this->t().'// 数据库连接配置'.$this->n().
        $this->t().'\'db\' => array('.$this->n().
        $this->t(2).'\'default\' => array('.$this->n().
        $this->t(3).'\'dsn\' => \'mysql:dbname=app;host=127.0.0.1;charset=utf8\','.$this->n().
        $this->t(3).'\'username\' => \'root\','.$this->n().
        $this->t(3).'\'password\' => \'******\','.$this->n().
        $this->t(3).'\'prefix\' => \'tb_\','.$this->n().
        $this->t(2).'),'.$this->n().
        $this->t(2).'\'server\' => array('.$this->n().
        $this->t(3).'\'dsn\' => \'mysql:dbname=app;host=remote.dbhost.com;charset=utf8\','.$this->n().
        $this->t(3).'\'username\' => \'dbmanager\','.$this->n().
        $this->t(3).'\'password\' => \'******\','.$this->n().
        $this->t(3).'\'prefix\' => \'tb_\','.$this->n().
        $this->t(2).')'.$this->n().
        $this->t().'),'.$this->n().$this->n().
        $this->t().'// 定义数据库连接所用到的标识'.$this->n().
        $this->t().'\'connect\' => \'default\','.$this->n(2).
        $this->t().'// 身份验证密钥串'.$this->n().
        $this->t().'\'id_key\' => \''.md5(microtime()).'\','.$this->n(2).
        $this->t().'//-------------------自定义常量分割线------------------------'.$this->n(2).
        ');';
        $this->createFile(SC_CONFIG_PATH.'/config.php', $tpl, 0777);
    }
    
    private function createDefaultControllerFile() {
        $tpl = '<?php'.$this->n(2).
        '/**'.$this->n().
        ' * 默认控制器'.$this->n().
        ' * @author scabish'.$this->n().
        ' * @since '.date('Y-m-d H:i:s').$this->n().
        ' */'.$this->n().
        'class IndexController extends SCController {'.$this->n(2).
            $this->t().'public function __construct() {'.$this->n().
                $this->t(2).'parent::__construct();'.$this->n().
            $this->t().'}'.$this->n().$this->n().
            $this->t().'public function index() {'.$this->n().
                $this->t(2).'$this->view->render(get_defined_vars());'.$this->n().
            $this->t().'}'.$this->n().
        '}';
        $this->createFile(SC_CONTROLLER_PATH.'/IndexController.class.php', $tpl, 0775);
    }
    
    private function createDefaultViewFile() {
        $tpl = '<!Doctype html>'.$this->n().
        '<html>'.$this->n().
        '<head>'.$this->n().
        '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'.$this->n().
        '<title>Hello, world!</title>'.$this->n().
        '<link rel="stylesheet" type="text/css" href="<?php echo SCS::url()->base ?>/asset/css/style.css" />'.$this->n().
        '</head>'.$this->n().
        '<body>'.$this->n().
        '<div class="wrapper">'.$this->n(2).
        '<?php $this->widget(\'header\'); ?>'.$this->n(2).
        '<div class="content">'.$this->n().
            $this->t().'<h2>Hello, world!</h2>'.$this->n().
        '</div>'.$this->n(2).
        '<?php $this->widget(\'footer\'); ?>'.$this->n(2).
        '</div>'.$this->n().
        '</body>'.$this->n().
        '</html>';
        $this->createFile(SC_VIEW_PATH.'/index/index.php', $tpl, 0775);
    }
    
    private function createDefaultStyleFile() {
        $tpl = '@CHARSET "UTF-8";'.$this->n().
        '*{margin:0;padding:0;text-decoration:none;}'.$this->n().
        'body{font-family:"microsoft yahei",Monaco,Consolas;font-size:100%;text-align:left;color:#130c0e;background:#fffffb;}'.$this->n().
        'body .wrapper{padding:1em;}'.$this->n().
        'body .content{font-size:2em;}'.$this->n().
        'a{color:#009ad6;}';
        $this->createFile(SC_ASSET_PATH.'/css/style.css', $tpl, 0775);
    }
    
    private function createDefaultWidgetFile() {
        $header = '<div class="header">'.$this->n(2).'</div>';
        $this->createFile(SC_VIEW_PATH.'/widget/header.php', $header, 0775);
        
        $footer = '<div class="footer">'.$this->n().
        $this->t().'@<a href="http://git.oschina.net/pycorvn/scabish">scabish <?php echo SCScabish::VERSION ?></a>'.$this->n().
        '</div>';
        $this->createFile(SC_VIEW_PATH.'/widget/footer.php', $footer, 0775);
    }
    
    private function showSuccess() {
        header('Content-Type:text/html;charset=utf-8');
        die('项目初始化成功！');
    }
    
    private function createFile($filePath, $content, $mode) {
        file_exists($filePath) && is_file($filePath) && @unlink($filePath);
        touch($filePath) && chmod($filePath, $mode);
        file_put_contents($filePath, $content);
    }
    
    private function t($n = 1) {
        return str_repeat(' ', 4*$n);
    }
    
    private function n($n = 1) {
        return str_repeat(PHP_EOL, $n);
    }
}